function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import { useCallback, useContext, useRef } from 'react';
import { Search } from "../../../baseComponent";
import { BaseHelp, HelpContext, Panel, Table, TableSelectionModel } from "../../../functionalComponent";
import { Layout, iup, useRefCallback } from "../../../util";
import CalcSubProjectTree from "./CalcSubProjectTree";
import { findFilterIndexes } from "./utils";
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
var tableProps = {
  rowSelected: false,
  rowChecked: true,
  cache: false,
  headerMenu: false,
  style: {
    borderStyle: 'solid',
    borderColor: 'var(--border-color-split, #f0f0f0)',
    borderWidth: 1
  }
};
var tableStyle = {
  borderStyle: 'solid',
  borderColor: 'var(--border-color-split, #f0f0f0)',
  borderWidth: 1
};
var Header = BaseHelp.Header,
  Footer = BaseHelp.Footer,
  MultipleButtons = BaseHelp.MultipleButtons;

/**
 * 核算项目帮助content
 * @returns
 */
export function HelpContent() {
  var _useContext = useContext(HelpContext),
    request = _useContext.request,
    ok = _useContext.ok,
    _useContext$contentPa = _useContext.contentParams,
    _useContext$contentPa2 = _useContext$contentPa.showTree,
    showTree = _useContext$contentPa2 === void 0 ? false : _useContext$contentPa2,
    getFieldValue = _useContext$contentPa.getFieldValue,
    columns = _useContext$contentPa.columns,
    helpTitle = _useContext$contentPa.helpTitle,
    helpId = _useContext$contentPa.helpId,
    selectedArray = _useContext$contentPa.selectedArray,
    multiple = _useContext$contentPa.multiple,
    footer = _useContext$contentPa.footer,
    treeProps = _useContext$contentPa.treeProps,
    locale = _useContext.locale,
    randomKey = _useContext.randomKey;
  var btnRef = useRef();
  var rightTable = useRef();
  var MAIN_TABLE_KEY = "".concat(helpId, "_").concat(randomKey, "_main");
  var SUB_TABLE_KEY = "".concat(helpId, "_").concat(randomKey, "_sub");

  // 表格
  var _getTable = useCallback(function (tableKey) {
    return tableKey && iup.compIns[tableKey].getApi();
  }, []);
  var getResult = function getResult() {
    if (multiple) {
      var result = rightTable.current.getApi().getRows().map(function (r) {
        return {
          value: getFieldValue(r),
          label: getFieldValue(r, 'label'),
          origin: _objectSpread({}, r)
        };
      });
      return result.length > 0 ? result : undefined;
    } else {
      var value = _getTable(SUB_TABLE_KEY).getSelectedData()[0];
      if (value) {
        return {
          value: getFieldValue(value),
          label: getFieldValue(value, 'label'),
          origin: _objectSpread({}, value)
        };
      }
      return undefined;
    }
  };
  var toRight = function toRight(values) {
    if (multiple) {
      btnRef.current.toRight(values);
    } else {
      ok(getResult());
    }
  };
  var onSearch = function onSearch(value) {
    _getTable(MAIN_TABLE_KEY).query({
      keyword: value
    });
  };

  // 默认选中
  var onDataLoad = useRefCallback(function (table) {
    var _ref = table.getState() || {},
      _ref$dataSource = _ref.dataSource,
      dataSource = _ref$dataSource === void 0 ? [] : _ref$dataSource;
    var selectIndex = findFilterIndexes(dataSource, function (it) {
      return (selectedArray || []).map(function (x) {
        return x.id;
      }).includes(it.id);
    });
    table.setSelected(selectIndex);
  });

  // 子表数据data
  var subTableRequest = useRefCallback(function (params) {
    if (!params.selectId) {
      return Promise.resolve({
        total: 0,
        list: []
      });
    }
    return request(params);
  });
  return /*#__PURE__*/_jsxs(Layout, {
    children: [/*#__PURE__*/_jsx(Header, {
      title: helpTitle || '核算项目帮助'
    }), /*#__PURE__*/_jsxs(Layout.Flex, {
      direction: "row",
      style: {
        padding: 5
      },
      children: [showTree && /*#__PURE__*/_jsx(Layout.Slider, {
        size: 240,
        children: /*#__PURE__*/_jsx(CalcSubProjectTree, {
          getTable: function getTable() {
            return _getTable(MAIN_TABLE_KEY);
          },
          getSubTable: function getSubTable() {
            return _getTable(SUB_TABLE_KEY);
          },
          multiple: multiple,
          treeProps: treeProps,
          showTree: showTree
        })
      }), /*#__PURE__*/_jsxs(Layout.Flex, {
        direction: "column",
        style: {
          marginLeft: 8
        },
        children: [/*#__PURE__*/_jsx(Search, {
          size: "small",
          allowClear: true,
          placeholder: locale.searchPlaceholder,
          onSearch: onSearch,
          style: {
            marginBottom: 8,
            width: 300,
            alignSelf: 'flex-end'
          }
        }), /*#__PURE__*/_jsxs(Layout.Flex, {
          direction: "column",
          children: [/*#__PURE__*/_jsx(Layout.Flex, {
            children: /*#__PURE__*/_jsx(Table, {
              style: tableStyle,
              id: MAIN_TABLE_KEY,
              columns: columns,
              pagination: {
                hideOnSinglePage: false
              },
              autoLoad: true,
              request: request,
              response: function response(res) {
                var _res$total;
                return {
                  total: (_res$total = res.total) !== null && _res$total !== void 0 ? _res$total : 0,
                  record: res.list || []
                };
              },
              rowSelection: {
                type: TableSelectionModel.ROW
              },
              onRow: function onRow(_, __, row) {
                return {
                  onClick: function onClick() {
                    return _getTable(SUB_TABLE_KEY).setExtraParam({
                      selectId: row.projCode
                    });
                  }
                };
              }
            })
          }), /*#__PURE__*/_jsx(Layout.Slider, {
            size: 200,
            children: /*#__PURE__*/_jsxs(Layout.Flex, {
              direction: "column",
              children: [/*#__PURE__*/_jsx(Panel, {
                title: '核算项目',
                bodyStyle: {
                  padding: 0,
                  height: '100%'
                },
                style: {
                  marginBottom: 0
                }
              }), /*#__PURE__*/_jsx(Layout.Flex, {
                children: /*#__PURE__*/_jsx(Table, {
                  style: tableStyle,
                  id: SUB_TABLE_KEY,
                  columns: columns,
                  pagination: {
                    hideOnSinglePage: false
                  },
                  autoLoad: false,
                  request: subTableRequest,
                  response: function response(res) {
                    var _res$total2;
                    return {
                      total: (_res$total2 = res.total) !== null && _res$total2 !== void 0 ? _res$total2 : 0,
                      record: res.list || []
                    };
                  },
                  onDataLoad: onDataLoad,
                  rowSelection: {
                    type: multiple ? [TableSelectionModel.CHECKBOX, TableSelectionModel.MULTIPLE_INTERVAL] : TableSelectionModel.ROW
                  },
                  onRow: function onRow(rowIndex, table) {
                    return {
                      onDoubleClick: function onDoubleClick() {
                        return toRight([table.getRow(rowIndex)]);
                      }
                    };
                  }
                })
              })]
            })
          })]
        })]
      }), multiple && /*#__PURE__*/_jsx(MultipleButtons, {
        getRowKey: getFieldValue,
        outRef: btnRef,
        getActiveTable: function getActiveTable() {
          return _getTable(SUB_TABLE_KEY);
        },
        getResultTable: function getResultTable() {
          return rightTable.current.getApi();
        },
        style: {
          alignItems: 'flex-end'
        }
      }), multiple && /*#__PURE__*/_jsx(Layout.Slider, {
        size: 200,
        children: /*#__PURE__*/_jsx(Table, _objectSpread(_objectSpread({}, tableProps), {}, {
          columns: columns,
          dataSource: selectedArray,
          ref: rightTable,
          onRow: function onRow(rowIndex) {
            return {
              onDoubleClick: function onDoubleClick() {
                return btnRef.current.toLeft(rowIndex);
              }
            };
          }
        }))
      })]
    }), /*#__PURE__*/_jsx(Footer, {
      getResult: getResult,
      children: footer && /*#__PURE__*/_jsx("span", {
        style: {
          flex: 1,
          width: 0
        },
        children: footer
      })
    })]
  });
}